﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution50
{
    public double MyPow1(double x, int n)
    {

        while (n % 2 == 0 && n != 0)//偶数
        {

            n = n / 2;
            x = x * x;

        }

        return Math.Pow(x, n);//这个函数有点画蛇添足
    }

    //递归
    public double MyPow2(double x, int n)
    {

        long N = n;
        if (N < 0)
        {
            return 1 / quickMul(x, -N);

        }
        else
        {
            return quickMul(x, N);
        }

    }

    public double quickMul2(double x, long N)
    {

        if (N == 0)
        {
            return 1.0;
        }

        double y = quickMul(x, N / 2);//

        if (N % 2 == 0)//偶数
        {
            return y * y;

        }
        else
        {
            return y * y * x;
        }



    }

    //迭代
    public double MyPow3(double x, int n)
    {

        double y = 0;
        while (n != 0)
        {
            n = n / 2;
            double s = x * x;

            if (n % 2 == 0)//偶数
            {

                y = s;


            }
            else
            {

                y = x * s;


            }

            x = s;

        }
        return y;

    }

    //迭代加二进制
    public double MyPow(double x, int n)
    {

        long N = n;
        if (N < 0)
        {
            return 1 / quickMul(x, -N);

        }
        else
        {
            return quickMul(x, N);
        }

    }

    public double quickMul(double x, long N)
    {
        double res = 1.0;
        double xs = x;
        while (N != 0)
        {


            if (N % 2 == 1)//奇数数
            {


                res = res * xs;


            }


            xs = xs * xs;
            N = N / 2;


        }
        return res;

    }
}

    class Program50
    {
        static void Main50(string[] args)
        {
            Solution50 solution = new Solution50();
            double res = solution.quickMul(2, 10);
            Console.WriteLine(res);


        }
    }

